package com.mijie.homi.dao.topic;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mijie.core.IBatisDao;
import com.mijie.homi.common.domain.topic.Topic;
import com.mijie.homi.common.domain.topic.TopicType;
import com.mijie.homi.common.domain.user.User;
import com.mijie.homi.common.util.HomiUtil;
import com.mijie.util.DateUtil;

@Repository
@Transactional
public class TopicDao extends IBatisDao {

	public int postTopic(Topic topic){
		return this.insert("postTopic", topic);
	}
	
	public Map<Integer,Topic> listTopicMapByIds(List<Integer> list){
		return this.selectMap("listTopicMapByIds", list, "id");
	}
	
	public Topic getTopicById(int id){
		return this.selectOne("getTopicById", id);
	}
	
	public int setTimelineIdForTopic(int id,int timelineId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("timelineId", timelineId);
		parameter.put("id", id);
		return this.update("setTimelineIdForTopic", parameter);
	}
	
	public int anonymousTopic(int author,int id){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("author", author);
		parameter.put("id", id);
		return this.update("anonymousTopic", parameter);
	}
	
	public int addConfusedUser(int topicId,int userId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getTopicConfusedTableName(topicId));
		parameter.put("topicId", topicId);
		parameter.put("userId", userId);
		return this.insert("addConfusedUser", parameter);
	}
	
	public int removeConfusedUser(int topicId,int userId){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getTopicConfusedTableName(topicId));
		parameter.put("topicId", topicId);
		parameter.put("userId", userId);
		return this.delete("removeConfusedUser", parameter);
	}
	
	public List<User> listTopicConfused(int topicId,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getTopicConfusedTableName(topicId));
		parameter.put("topicId", topicId);
		parameter.put("limit", limit);
		return this.selectList("listTopicConfused", parameter);
	}
	
	
	public int incrNumOfConfused(int id){
		return this.update("incrNumOfConfused", id);
	}
	
	public int decrNumOfConfused(int id){
		return this.update("decrNumOfConfused",id);
	}
	
	public int incrTopicNumOfParticipate(int id){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("lastAccessAt", new Date());
		return this.update("incrTopicNumOfParticipate",parameter);
	}
	
	public int increTopicNumOfOpinion(int id){
		return this.update("increTopicNumOfOpinion",id);
	}
	
	public int decrTopicNumOfOpinion(int id){
		return this.update("decrTopicNumOfOpinion",id);
	}
	
	public int incrTopicNumOfComment(int id){
		return this.update("incrTopicNumOfComment",id);
	}
	
	public int decrTopicNumOfComment(int id){
		return this.update("decrTopicNumOfComment",id);
	}
	
	public void addTopicTagForUser(int userId,String tag){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("userId", userId);
		parameter.put("tag", tag);
		this.insert("addTopicTagForUser",parameter);
	}
	
	public String getTopicTagForUser(int userId){
		return this.selectStr("getTopicTagForUser", userId);
	}
	
	public int updateVisible(int id,int pass){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("id", id);
		parameter.put("visible", pass);
		parameter.put("verifyAt", new Date());
		return this.update("updateVisible",parameter);
	}
	
	public int deleteTopic(int id){
		return this.delete("deleteTopic",id);
	}
	
	public List<TopicType> listTopicType(){
		return this.selectList("listTopicType");
	}
	
	public List<Topic> fetchTopicByType(int type,int offset,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getTopicInTypeTableName(type));
		parameter.put("offset", offset);
		parameter.put("limit", limit);
		return this.selectList("fetchTopicByType", parameter) ;
	}
	
	public List<Integer> listTopicIdByType(int type,Date pointInTime,int offset,int limit){
		HashMap<String,Object> parameter = new HashMap<String,Object>();
		parameter.put("tableName", HomiUtil.getTopicInTypeTableName(type));
		parameter.put("pointInTime", DateUtil.date2str(pointInTime, true));
		parameter.put("offset", offset);
		parameter.put("limit", limit);
		return this.selectIntList("listTopicIdByType", parameter) ;
	}
}
